package com.tutor.lzm.function.teacher.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tutor.lzm.AppSession;
import com.tutor.lzm.R;
import com.tutor.lzm.activity.BaseActivity;
import com.tutor.lzm.app.CommonValue;
import com.tutor.lzm.bean.TeacherExperienceBean;
import com.tutor.lzm.http.BaseResponse;
import com.tutor.lzm.http.HttpClient;
import com.tutor.lzm.http.request.UpdateTeacherSeniorityRequest;
import com.tutor.lzm.view.dialog.DateDialog;
import com.tutor.syslib.Logs;
import com.tutor.syslib.util.DateTimeHelper;
import com.tutor.syslib.util.MessageTools;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zzgpc on 2017-9-23.
 * 添加老师教育经历
 */
public class AddTeacherExperienceAct extends BaseActivity {
    private String tag = "AddTeacherExperienceAct";

    @BindView(R.id.et_description_title)
    EditText etDescriptionTitle;
    @BindView(R.id.img_delete_title)
    ImageView imgDeleteTitle;
    @BindView(R.id.tv_start_time_show)
    TextView tvStartTimeShow;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.rl_start_time)
    RelativeLayout rlStartTime;
    @BindView(R.id.tv_end_time_show)
    TextView tvEndTimeShow;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.rl_end_time)
    RelativeLayout rlEndTime;
    @BindView(R.id.et_description)
    EditText etDescription;

    TeacherExperienceBean bean;


    public static void start(Context context, TeacherExperienceBean bean) {
        Intent intent = new Intent(context, AddTeacherExperienceAct.class);
        if (bean != null) {
            intent.putExtra("data", bean);
        }
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_add_teacher_experience);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        if (getIntent().hasExtra("data")) {
            try {
                bean = (TeacherExperienceBean) getIntent().getSerializableExtra("data");
                initTitle("修改教育经历", "保存", rightListener);
            } catch (Exception e) {
                Logs.e(tag, e.getMessage());
            }

        } else {
            initTitle("新增教育经历", "保存", rightListener);
        }
        if (bean != null) {
            //有数据就显示数据
            etDescriptionTitle.setText(bean.title);
            tvStartTime.setText(bean.startDate);
            tvEndTime.setText(bean.endDate);
            etDescription.setText(bean.description);
        } else

        {
            bean = new TeacherExperienceBean();
        }


    }

    @OnClick({R.id.img_delete_title, R.id.rl_start_time, R.id.rl_end_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_delete_title:
                etDescriptionTitle.setText("");
                break;
            case R.id.rl_start_time:
                selectDate(tvStartTime);
                break;
            case R.id.rl_end_time:
                selectDate(tvEndTime);
                break;
        }
    }


    /**
     * 保存
     */
    private void save() {
        String startDate = tvStartTime.getText().toString().trim();
        String endDate = tvEndTime.getText().toString().trim();
        String title = etDescriptionTitle.getText().toString().trim();
        String description = etDescription.getText().toString().trim();
        bean.startDate = startDate;
        bean.endDate = endDate;
        bean.title = title;
        bean.description = description;
        UpdateTeacherSeniorityRequest request = new UpdateTeacherSeniorityRequest();
        request.teacherSeniorityId = bean.teacherSeniorityId;
        request.teacherId = AppSession.getInstance().getUserId();
        request.timeFrom = startDate;
        request.timeTo = endDate;
        request.title = title;
        request.teachDesc = description;
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                EventBus.getDefault().post(bean);
                MessageTools.showToast(oThis, "保存成功");
                finish();
            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                MessageTools.showToast(oThis, msg);
            }
        });
    }


    /**
     * 选择日期
     */
    private void selectDate(final TextView textView) {
        new DateDialog(this, textView.getText().toString().trim(), new DateDialog.DateDialogValue() {

            @Override
            public boolean findDateDialogValue(final String date) {
                String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
                Long diffDate = DateTimeHelper.getDaysOfTowDiffDate(date, today);
                if (diffDate < 0) {
                    Toast.makeText(getApplicationContext(), "请设置今天之前的日期", Toast.LENGTH_SHORT).show();
                    return false;
                }
                textView.setText(date);
                return true;
            }
        }).builder().setPositiveButton("确定", null).setNegativeButton("取消", null).show();
    }


    private View.OnClickListener rightListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            save();
        }
    };
}
